package com.cnhis.cloudhealth.clinical.clidoctor.patientsearch.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.clidoctor.cliparamter.dao.CliParamterDao;
import com.cnhis.cloudhealth.clinical.clidoctor.patientsearch.dao.PatientSearchDao;
import com.cnhis.cloudhealth.clinical.commons.utils.SqlHelper;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

@Service
public class PatientSearchBo
{
    @Resource
    private PatientSearchDao dao;
    
    @Resource
    private CliParamterDao cliParamterDao;
    
    @Resource
    private GetNewId getNewId;
    
    public ModelVo querySearchSickCondition()
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            list = dao.querySearchSickCondition();
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    @SuppressWarnings({"rawtypes", "unchecked"})
    public ModelVo queryWaitingAndTransferTreatment(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            HashMap hs = new HashMap();
            hs.put("productid", 100);
            hs.put("programid", 105002);
            hs.put("paramno", 238);
            String value = cliParamterDao.getSysParamValue(hs);
            map.put("lPara2", value == null || "".equals(value) ? "0" : value);
            pg = dao.queryWaitingAndTransferTreatment(map, currPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRecords(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo queryVisitDoctor(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = dao.queryVisitDoctor(map);
            mo.setList(list);
            mo.setRows(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        
    }
    
    public ModelVo queryAllJiezhen(Map map)
        throws Exception
    {
            ModelVo mo = new ModelVo();
            List list = dao.queryForList("PatientSearch.queryAllJiezhen", map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        
        
    }
    
    public ModelVo getDoctorAdvicePatient(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = dao.getDoctorAdvicePatient(map, currPage, pageSize);
            
        }
        catch (Exception e)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(pg.getList());
        mo.setRows(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }
    
    public ModelVo getPinYinCodeSearch(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = dao.getPinYinCodeSearch(map, currPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo findDepartments(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            list = dao.findDepartments(map);
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    @SuppressWarnings({"unchecked", "rawtypes"})
    public ModelVo getPatientInfo(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            
            String value = cliParamterDao.getSysParamValue(map);
            if (value != null && !"".equals(value))
            {
                if ("0".equals(value))
                {
                    map.put("aDate", "1899-12-30");
                }
                else
                {
                    value = String.valueOf(-(Integer.parseInt(value) - 1));
                    String adate = dao.getDate(value);
                    map.put("aDate", adate);
                }
            }
            else
            {
                map.put("aDate", "1899-12-30");
            }
            list = dao.getPatientInfo(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    public ModelVo patientOrderSubsequentVisitItemDemand(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = dao.patientOrderSubsequentVisitItemDemand(map, currPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo getPatientByVIPId(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            list = dao.getPatientByVIPId(map);
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    public ModelVo findWaitDoctorPatient(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            list = dao.findWaitDoctorPatient(map);
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    public ModelVo findSeeDoctorPatient(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            list = dao.findSeeDoctorPatient(map);
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo simpleRegisterSearchPatient(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            if ("a.VAA03".equals(map.get("FieldName").toString()))
            {
                String context = dao.setContext(map);
                map.put("context", context);
            }
            list = dao.simpleRegisterSearchPatient(map);
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo medicalSearchPatient(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            String value = dao.getiak05(map);
            map.put("IAK05", value == null ? "" : value);
            
            list = dao.medicalSearchPatient(map);
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    public ModelVo seeDoctorHistory(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            
            list = dao.seeDoctorHistory(map);
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    public ModelVo isMedicalInsurance(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            
            list = dao.isMedicalInsurance(map);
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }

    public ModelVo findDepartmentsZhuyuan()
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            list = dao.findDepartmentsZhuyuan();
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }

    public ModelVo findPatentVao1List(HashMap map)
    {
        ModelVo modelVo = new ModelVo();
        try
        {
            List list = dao.findPatentVao1List(map);
            modelVo.setList(list);
            modelVo.setResult(StaticKeys.OH_SUCCESS);
            return modelVo;
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public ModelVo findSysparams(HashMap hs)
    {
        ModelVo modelVo = new ModelVo();
        List list = new ArrayList<>();
        try
        {
            //如果当前用户id对应的参数不存在，则用sys_parameters的value值
            //否则用sys_userparameters的value值
            List bckList = new ArrayList<>();
            //查询科室参数
            bckList = dao.queryForList("PatientSearch.findBck1params", hs);
            //查询个人参数和系统参数
            list = dao.queryForList("PatientSearch.findSysparams_user", hs);
            //合并科室参数和个人参数和系统参数
            List resultList = new ArrayList<>();
            resultList.addAll(list);
            resultList.addAll(bckList);
            modelVo.setList(resultList);
            modelVo.setResult(StaticKeys.OH_SUCCESS);
            return modelVo;
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
    }

    @SuppressWarnings({"unchecked", "unused", "rawtypes"})
    public ModelVo updateAllSysparams(HashMap hs)
    {
        ModelVo modelVo = new ModelVo();
        try
        {
            List resultArr105002_1List = JsonUtil.getList(hs.get("resultArr105002_1").toString());
            List resultArr105002_0List = JsonUtil.getList(hs.get("resultArr105002_0").toString());
            List resultArr9999_1List = JsonUtil.getList(hs.get("resultArr9999_1").toString());
            List resultArr9999_0List = JsonUtil.getList(hs.get("resultArr9999_0").toString());
            List resultArrValueList = JsonUtil.getList(hs.get("resultArrValue").toString());
            hs.put("resultArr105002_1", resultArr105002_1List);
            hs.put("resultArr105002_0", resultArr105002_0List);
            hs.put("resultArr9999_1", resultArr9999_1List);
            hs.put("resultArr9999_0", resultArr9999_0List);
            hs.put("resultArrValue", resultArrValueList);
            
            List<Map<String, Object>> allArr = JsonUtil.getList(hs.get("allArr").toString());
            //个人参数更新
            List<Map<String, Object>> userList = new ArrayList<>();
/*            List<String> mapList = new ArrayList<String>();
            mapList.add("106");
            mapList.add("107");
            mapList.add("243");
            mapList.add("351");
            mapList.add("389");
            mapList.add("392");
            mapList.add("386");
            mapList.add("393");
            mapList.add("100");
            mapList.add("246");
            mapList.add("394");
            mapList.add("401");
            mapList.add("402");
            
            //科室参数
            mapList.add("108");//西药
            mapList.add("109");//
            mapList.add("110");//草药
            Set<String> bck1ParamsSet = new HashSet<String>();
            bck1ParamsSet.add("108");
            bck1ParamsSet.add("109");
            bck1ParamsSet.add("110");*/
            for(Map<String, Object> temp : allArr)
            {
                if("100".equals(temp.get("productid").toString()) && "105002".equals(temp.get("programid").toString()) && "13".indexOf(temp.get("privilege").toString())>=0)
                {
//                    if(mapList.contains(temp.get("paramno").toString()))
//                    {
                        userList.add(temp);
//                    }
                }
            }
            for(Map<String, Object> temp : userList)
            {
                String productid = temp.get("productid").toString();
                String programid = temp.get("programid").toString();
                String paramno = temp.get("paramno").toString();
                String privilege = temp.get("privilege").toString();
                temp.put("paramid", Long.parseLong(productid+programid+paramno));
                temp.put("id", getNewId.nextId());
                if ("1".equals(privilege))
                {
                    temp.put("userid", hs.get("userid").toString());
                }
                else if("3".equals(privilege))
                {
                    temp.put("userid", hs.get("bck01").toString());
                }
                List hasTemp = dao.queryForList("PatientSearch.hasUserParam", temp);
                if(hasTemp==null || hasTemp.isEmpty())
                {
                    dao.insert("PatientSearch.insertUserParam", temp);
                }
                else
                {
                    dao.update("PatientSearch.updateUserParam", temp);
                }
            }
            List list = dao.updateAllSysparams(hs);
            modelVo.setList(list);
            modelVo.setResult(StaticKeys.OH_SUCCESS);
            return modelVo;
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public ModelVo findYaofang(HashMap hs)
    {
        ModelVo modelVo = new ModelVo();
        try
        {
            List list = dao.findYaofang(hs);
            modelVo.setList(list);
            modelVo.setResult(StaticKeys.OH_SUCCESS);
            return modelVo;
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
    }

    public ModelVo findSysparams1(HashMap hs)
    {
        ModelVo modelVo = new ModelVo();
        List list = new ArrayList<>();
        try
        {
          //如果当前用户id对应的参数不存在，则用sys_parameters的value值
            //否则用sys_userparameters的value值
            hs.put("paramid", "100105002392");//主题参数
            List hasTemp = dao.queryForList("PatientSearch.hasUserParam", hs);
            if(hasTemp==null || hasTemp.isEmpty())
            {
                list = dao.findSysparams1(hs);
            }
            else
            {
                list = dao.queryForList("PatientSearch.findSysparams1_user", hs);
            }
            
            modelVo.setList(list);
            modelVo.setResult(StaticKeys.OH_SUCCESS);
            return modelVo;
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
    }

    public ModelVo querywaitingandtransfertreatment_one(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            HashMap hs = new HashMap();
            hs.put("productid", 100);
            hs.put("programid", 105002);
            hs.put("paramno", 238);
            String value = cliParamterDao.getSysParamValue(hs);
            map.put("lPara2", value == null || "".equals(value) ? "0" : value);
            List list = dao.querywaitingandtransfertreatment_one(map);
            mo.setList(list);
            mo.setUserdata(  
                SqlHelper.getNamespaceSql(  
                    cliParamterDao.getSqlSession(),  
                        "PatientSearch.querywaitingandtransfertreatment_one",map));
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public ModelVo updateUserParams_cancel(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List<Map<String, Object>> cancelUserList = JsonUtil.getList(hs.get("cancelArr_user").toString());
            List<Map<String, Object>> cancelPublicList = JsonUtil.getList(hs.get("cancelArr_public").toString());
            hs.put("cancelUserList", cancelUserList);
            hs.put("cancelPublicList", cancelPublicList);
            dao.updateUserParams_cancel(hs);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo findDepartments2(Map map)
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            list = dao.findDepartmentsZhuyuan2();
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }

    public ModelVo findYaofang1(HashMap hs)
    {
        ModelVo modelVo = new ModelVo();
        try
        {
            List list1 = dao.findXiYaofang(hs);
            List list2 = dao.findChengYaofang(hs);
            List list3 = dao.findZHongYaofang(hs);
            List list4 = dao.findCailiaofang(hs);
            
            Map<String,Object> temp = new HashMap<String,Object>();
            temp.put("xiyaofang", list1);
            temp.put("chengyaofang", list2);
            temp.put("zhongyaofang", list3);
            temp.put("cailiaofang", list4);
            modelVo.setMap(temp);
            modelVo.setResult(StaticKeys.OH_SUCCESS);
            return modelVo;
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
        
    }

    public ModelVo queryVac34(HashMap hs)
    {
        ModelVo modelVo = new ModelVo();
        try
        {
            List list = dao.queryVac34(hs);
            Map<String,Object> temp = new HashMap<String,Object>();
            modelVo.setList(list);
            modelVo.setResult(StaticKeys.OH_SUCCESS);
            return modelVo;
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
        
    }

    public ModelVo queryVaj1ByVac01(HashMap hs)
    {
        ModelVo modelVo = new ModelVo();
        try
        {
            List list = dao.queryForList("PatientSearch.queryVaj1ByVac01", hs);
            Map<String,Object> temp = new HashMap<String,Object>();
            modelVo.setList(list);
            modelVo.setResult(StaticKeys.OH_SUCCESS);
            return modelVo;
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            e.printStackTrace();
            return modelVo;
        }
    }
}
